// 表单验证工具函数

// 邮箱格式验证
export function validateEmail(email: string): boolean {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    return emailRegex.test(email)
}

// 用户名验证（3-20位，只能包含字母、数字、下划线）
export function validateUsername(username: string): boolean {
    const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/
    return usernameRegex.test(username)
}

// 密码强度验证
export function validatePassword(password: string): {
    isValid: boolean
    strength: 'weak' | 'medium' | 'strong'
    issues: string[]
} {
    const issues: string[] = []
    let score = 0

    // 长度检查
    if (password.length < 8) {
        issues.push('Password must be at least 8 characters')
    } else {
        score += 1
    }

    // 包含小写字母
    if (!/[a-z]/.test(password)) {
        issues.push('Must contain lowercase letters')
    } else {
        score += 1
    }

    // 包含大写字母
    if (!/[A-Z]/.test(password)) {
        issues.push('Must contain uppercase letters')
    } else {
        score += 1
    }

    // 包含数字
    if (!/\d/.test(password)) {
        issues.push('Must contain numbers')
    } else {
        score += 1
    }

    // 包含特殊字符
    if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
        issues.push('Recommended to contain special characters')
    } else {
        score += 1
    }

    let strength: 'weak' | 'medium' | 'strong'
    if (score <= 2) {
        strength = 'weak'
    } else if (score <= 4) {
        strength = 'medium'
    } else {
        strength = 'strong'
    }

    return {
        isValid: password.length >= 8 && score >= 3,
        strength,
        issues
    }
}

// 确认密码验证
export function validatePasswordConfirmation(password: string, confirmation: string): boolean {
    return password === confirmation && password.length > 0
}// 图片文件验证
export function validateImageFile(file: File, options: {
    maxSize?: number // MB
    allowedTypes?: string[]
} = {}): {
    isValid: boolean
    error?: string
} {
    const {
        maxSize = 5,
        allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
    } = options

    // 检查文件类型
    if (!allowedTypes.includes(file.type)) {
        return {
            isValid: false,
            error: `Unsupported file format. Supported formats: ${allowedTypes.join(', ')}`
        }
    }

    // 检查文件大小
    const fileSizeMB = file.size / (1024 * 1024)
    if (fileSizeMB > maxSize) {
        return {
            isValid: false,
            error: `File size cannot exceed ${maxSize}MB. Current file size: ${fileSizeMB.toFixed(2)}MB`
        }
    }

    return {
        isValid: true
    }
}

// 手机号验证（中国大陆）
export function validatePhone(phone: string): boolean {
    const phoneRegex = /^1[3-9]\d{9}$/
    return phoneRegex.test(phone)
}

// URL验证
export function validateUrl(url: string): boolean {
    try {
        new URL(url)
        return true
    } catch {
        return false
    }
}

// 身份证号验证（简单版）
export function validateIdCard(idCard: string): boolean {
    const idCardRegex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    return idCardRegex.test(idCard)
}

// 验证码验证（4-6位数字）
export function validateVerificationCode(code: string): boolean {
    const codeRegex = /^\d{4,6}$/
    return codeRegex.test(code)
}